let storage = bui.storage();
let org;
let language;
let translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            if (language !== "chinese_simplified") {
                router.$("#fbHead").css('font-size', '.2rem')
                router.$("#nameFbZj").css('font-size', '.3rem')
                router.$("#fbFooter").css('font-size', '.18rem')
                router.$("#fbCountry").css('font-size', '.26rem')
            } else {
                router.$("#fbHead").css('font-size', '14px')
                router.$("#nameFbZj").css('font-size', '.34rem')
                router.$("#fbFooter").css('font-size', '12px')
                router.$("#fbCountry").css('font-size', '')
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#fbZjGzhLoading",
                width: 40,
                height: 40,
                text:"加载中。。。",
                autoClose: false,
                callback: function (argument) {
                }
            });

            let token = storage.get("token")
            org = storage.get("org",0)

            if (token) {
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        storage.set("dlTz", '9')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                })

                router.$("#fhZjFh").click(function () {
                    storage.set("tzPage", '0')
                    router.replace({ url: "pages/mainIndex/mainIndex.html"});
                })

                if (org) {
                    // uiLoading.show()
                    router.$("#fbCountry").html(org.description)
                    router.$("#csFb2").html(org.description)

                    router.$("#fbCountry").click(function () {
                        router.load({ url: "pages/fbCountry/fbCountry.html"});
                    })

                    let termListStore = storage.get("termList", 0)
                    if (!termListStore) {
                        uiLoading.show()
                        // 查询需要缓存数据
                        bui.ajax({
                            url: global.api.getCountryTermList,
                            method: "POST"
                        }).then(function(res){
                            let data = res.data
                            if (data){
                                let country = data.country
                                let countryList = []
                                country.forEach(coun => {
                                    if (coun.children && coun.children.length > 0) {
                                        countryList.push({id: coun.id, description: coun.description})
                                        storage.set("country" + coun.id, coun.children)
                                    }
                                })
                                storage.set("countryList", countryList)
                                let term = data.term
                                let termList = []
                                term.forEach(ter => {
                                    if (ter.children && ter.children.length > 0) {
                                        termList.push({id: ter.id, name: ter.name})
                                        storage.set("term" + ter.id, ter.children)
                                    }
                                })
                                setting(termList, global)
                                storage.set("termList", termList)
                                uiLoading.hide()
                            }
                        })
                    }  else {
                        setting(termListStore, global)
                    }
                    // bui.ajax({
                    //     url: global.api.getCountryList,
                    //     data: {type: '1', parent: '1'},
                    //     method: "POST"
                    // }).then(function(res){
                    //     let data = res.data
                    //     if (data) {
                    //         let html = ""
                    //         data.forEach((da, index) => {
                    //             if (index == 0) {
                    //                 html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'" style="border-left: red 2px solid; background-color: white">\n'+
                    //                     '           <p id="tabLabel'+index+'" class="tabLabel" style="color: red">'+da.name+'</p>\n'
                    //                 getTypeChildren(da.id, da.name,global.api.getCountryList)
                    //             } else {
                    //                 html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'">\n'+
                    //                     '           <p id="tabLabel'+index+'" class="tabLabel">'+da.name+'</p>\n'
                    //             }
                    //             html += '     </div>'
                    //         })
                    //         router.$("#fbGzhList").html(html)
                    //
                    //         router.$(".tab").click(function () {
                    //             // uiLoading.show()
                    //             let id = router.$(this).attr("id")
                    //             let xh = router.$(this).attr("name")
                    //             let mc = router.$(this).attr("mc")
                    //
                    //             if(router.$("#tabLabel" + xh).css('color') != 'red') {
                    //                 router.$(".tab").css({
                    //                     'border-left': 0,
                    //                     'background-color': 'white'
                    //                 });
                    //                 router.$(".tabLabel").css({
                    //                     'color': 'black'
                    //                 });
                    //
                    //                 router.$(this).css({
                    //                     'border-left': 'red 2px solid',
                    //                     'background-color': 'white'
                    //                 })
                    //                 router.$("#tabLabel" + xh).css({
                    //                     'color': 'red'
                    //                 })
                    //
                    //                 getTypeChildren(id, mc,global.api.getCountryList, uiLoading)
                    //             }
                    //         })
                    //     }
                    //     uiLoading.stop()
                    // })
                } else {
                    router.load({ url: "pages/fbCountry/fbCountry.html"});
                }
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '9')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }
            try {
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function getTypeChildren(id, mc,url, uiLoading) {
    let data = storage.get("term" + id, 0)
    let html = ""
    if (data) {
        let fontSize = '15px'
        if (language !== "chinese_simplified") {
            fontSize = '0.2rem'
        }

        let xb = 0;
        data.forEach((r,index) => {
            if (xb % 3 == 0) {
                html += '</div><div class="bui-box" style="height: 100%">'
            }
            xb++

            html += '<div class="span1 countryImg" id="'+r.id+'" name="'+r.parent+'" validSign="'+r.validSign+'">'+
                '<p class="cityText" style="font-size: '+fontSize+'">'+r.name+'</p></div>'

        })
        let len = data.length
        if (len % 3 != 0) {
            for (let i = 0; i < 3- (len%3); i++) {
                html += '<div class="span1"></div>'
            }
        }
        html += "</div>"
    }
    router.$("#fbGzhType").html(html)
    router.$(".countryImg").click(function () {
        let cId = $(this).attr("id")
        let pId = $(this).attr("name")
        let validSign = $(this).attr("validSign")
        storage.set('cateDo', {id: cId, parent: pId, validSign: validSign})
        router.load({ url: "pages/fbGzh/fbGzh.html"});
    })

    try {
        translate.execute();
    }catch (e) {
        console.log("切换语言报错")
    }
    // bui.ajax({
    //     url: url,
    //     data: {parent: parseInt(id)},
    //     method: "POST"
    // }).then(function(res){
    //     let data = res.data
    //     let html = ""
    //     if (data) {
    //         let xb = 0;
    //         data.forEach((r,index) => {
    //             if (xb % 3 == 0) {
    //                 html += '</div><div class="bui-box" style="height: 100%">'
    //             }
    //             xb++
    //
    //             html += '<div class="span1 countryImg" id="'+r.id+'" name="'+r.parent+'">'+
    //                 '<p>'+r.name+'</p></div>'
    //
    //         })
    //         let len = data.length
    //         if (len % 3 != 0) {
    //             for (let i = 0; i < 3- (len%3); i++) {
    //                 html += '<div class="span1"></div>'
    //             }
    //         }
    //         html += "</div>"
    //     }
    //     router.$("#fbGzhType").html(html)
    //     router.$(".countryImg").click(function () {
    //         let cId = $(this).attr("id")
    //         let pId = $(this).attr("name")
    //         storage.set('cateDo', {id: cId, parent: pId})
    //         router.load({ url: "pages/fbGzh/fbGzh.html"});
    //     })
    //
    //     if (uiLoading) {
    //         uiLoading.stop()
    //     }
    // })
}

function setting(termListStore, global) {
    let fontSize = '15px'
    if (language !== "chinese_simplified") {
        fontSize = '0.18rem'
    }

    let html = ""
    termListStore.forEach((da, index) => {
        if (index == 0) {
            html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'" style="border-left: red 2px solid; background-color: white">\n'+
                '           <p id="tabLabel'+index+'" class="cityText" style="color: red; font-size: '+fontSize+'">'+da.name+'</p>\n'
            getTypeChildren(da.id, da.name,global.api.getCountryList)
        } else {
            html += '<div class="tab" name="'+index+'" id="'+da.id+'" mc="'+da.name+'">\n'+
                '           <p id="tabLabel'+index+'" class="cityText" style="font-size: '+fontSize+'">'+da.name+'</p>\n'
        }
        html += '     </div>'
    })
    router.$("#fbGzhList").html(html)
    try {
        translate.execute();
    }catch (e) {
        console.log("切换语言报错")
    }

    router.$(".tab").click(function () {
        // uiLoading.show()
        let id = router.$(this).attr("id")
        let xh = router.$(this).attr("name")
        let mc = router.$(this).attr("mc")

        if(router.$("#tabLabel" + xh).css('color') != 'red') {
            router.$(".tab").css({
                'border-left': 0,
                'background-color': 'white'
            });
            router.$(".cityText").css({
                'color': 'black'
            });

            router.$(this).css({
                'border-left': 'red 2px solid',
                'background-color': 'white'
            })
            router.$("#tabLabel" + xh).css({
                'color': 'red'
            })

            getTypeChildren(id, mc,global.api.getCountryList)
        }
    })
}
